(in-package :procemu)

(defclass stack-instruction (instruction)
  ((place :accessor place))
  (:documentation "An instruction that manipulates the stack (push or pop)"))

(defmethod print-object ((instruction stack-instruction) stream)
  (print-unreadable-object (instruction stream :type t)
    (format stream "~A"
	    (location-string (place instruction)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defclass push (stack-instruction)
  ()
  (:documentation "Push the contents of a location onto the stack"))

(defmethod perform ((instruction push) (cpu cpu))
  (push (location cpu (place instruction))
	(stack cpu)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defclass pop (stack-instruction)
  ()
  (:documentation "Pop a number off the stack onto a location"))

(defmethod perform ((instruction pop) (cpu cpu))
  (setf (location cpu (place instruction))
	(pop (stack cpu))))